package com.fyzk.view;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.tyjc.lottery.R;

/**
 * 下拉菜单
 */
public class DownMenuPopupwindow extends PopupWindow implements View.OnTouchListener{

	private Context context;
	private String[] items;
	private View view;
	private ListView listView;
	private DownMenuPopupItemListener listener;
	private DownMenuPopupAdapter adapter;

	public DownMenuPopupwindow(Context context,int w,int h, String[] items){
		this(context, w, h, items, true);
	}

	public DownMenuPopupwindow(Context context,int w,int h, String[] items, boolean isTouchOutside){
		super(w, h);
		this.context = context;
		View view_pop = LayoutInflater.from(context).inflate(R.layout.popup_downmenu, null);
		this.view = view_pop;
		this.view.setOnTouchListener(this);
		this.items = items;
		initView();
		this.setContentView(view);
		this.setWidth(w);
		this.setHeight(h);
		this.setFocusable(isTouchOutside);
		this.setOutsideTouchable(isTouchOutside);
		// 刷新状态
		this.update();
		ColorDrawable dw = new ColorDrawable(0000000000);
		this.setBackgroundDrawable(dw);
		this.setAnimationStyle(R.style.showPopupAnimation);
	}


	@Override
	public boolean onTouch(View v, MotionEvent event) {
		this.dismiss();
		return true;
	}

	private void initView(){
		listView = (ListView) view.findViewById(R.id.downmenu_listview);
		adapter = new DownMenuPopupAdapter(items);
		listView.setEnabled(false);
		listView.setAdapter(adapter);
	}


	/**
	 * 显示popupWindow
	 *
	 * @param parent
	 */
	public void showPopupWindow(View parent) {
		if (!this.isShowing()) {
			// 以下拉方式显示popupwindow
//            this.showAsDropDown(parent, parent.getLayoutParams().width / 2, 18);
			if (null != listener) {
				listener.menuShow();
			}
			super.showAsDropDown(parent);
		} else {
			this.dismiss();
		}
	}

	private void popupDismiss(){
		this.dismiss();
	}

	@Override
	public void dismiss() {
		if (null != listener) {
			listener.menuDismiss();
		}
		super.dismiss();
	}

	//===== 接口	==========
	public interface DownMenuPopupItemListener {
		void onMenuItem(int position);
		void menuShow();
		void menuDismiss();
	}

	public void setDownMenuPopupOnItemListener(DownMenuPopupItemListener listener){
		this.listener = listener;
	}

	public void setItems(String[] items){
		this.items = items;
		adapter.setItems(items);
	}

	//==========================================================================
	class DownMenuPopupAdapter extends BaseAdapter{
		private String[] items;

		public DownMenuPopupAdapter(String[] items){
			this.items = items;
		}

		@Override
		public int getCount() {

			return items.length;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			String item = items[position];
			convertView = LayoutInflater.from(context).inflate(R.layout.adapter_downmenu, null);
			TextView text = (TextView) convertView.findViewById(R.id.downmenu_item_tv);

			text.setText(item);

			text.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					if (null != listener) {
						listener.onMenuItem(position);
					}

					popupDismiss();
				}
			});

			return convertView;
		}
		public void setItems(String[] items){
			this.items = items;
			this.notifyDataSetChanged();
		}

	}

}
